<?php
require_once("../includes/config.inc.php");
validate_admin();
if($_POST)
{
	@extract($_POST);
	if(is_array($chkIds))
	{
		$strIds =  @implode(",",$chkIds);

		$sqlNT = "  select * from tbl_nl_template  where templateId  = '$template' ";
		$resNT = executeQuery($sqlNT);
		if($rowSC = mysql_fetch_array($resNT))
		{
		    $subject   = $rowSC['subject'];
			$content   = $rowSC['message'];
    	//$fromEmail = $rowSC['fromEmail'];
			$fromText  = $rowSC['fromText'];
			$mailBody  = $content."<br>";
		}
		//echo $mailBody;
		ob_start();
		include("./mailTemplate.php");
		$mailTemplate = ob_get_contents();
		ob_clean();
		// echo $mailTemplate;

		$mailBody = nl2br($mailBody);
		$mailBody = str_replace("<MAIL_CONTENT>",$mailBody,$mailTemplate);

		if($Submit == 'Send Mail To All')
		{
			if(empty($template))
				{
					$_SESSION['sessMsg'] = "Please Select Template To Send Mail. ";
					header("location: addressBookManager.php");
					exit();
				}
			$sqlNS = " select * from tbl_address_book where status = '1' ";
			$resNS = executeQuery($sqlNS);
			while($row = mysql_fetch_array($resNS))
			{
				$mailBodySend = str_replace("&lt;SUBSCRIBER_NAME&gt;",$row['firstName'],$mailBody);
				send_mail($row['email'],$subject,$mailBodySend,"support@lakshyafoundation.org",$fromText,$html=true);
			}
			$_SESSION['sessMsg'] = " Newsletter Send To All Subscriber(s) Successfully. ";
			header("location: addressBookManager.php");
			exit();
		}
		
		if($Submit == 'Send Mail To Selected')
		{
			if(empty($template))
			{
				$_SESSION['sessMsg'] = "Please Select Template To Send Mail. ";
				header("location: addressBookManager.php");
				exit();
			}
			$sqlNS = " select * from tbl_address_book where id in ($strIds) ";
			$resNS = executeQuery($sqlNS);
			while($row = mysql_fetch_array($resNS))
			{
				$mailBodySend = str_replace("&lt;SUBSCRIBER_NAME&gt;",$row['firstName'],$mailBody);
				send_mail($row['email'],$subject,$mailBodySend,"support@lakshyafoundation.org",$fromText,$html=true);
			}
			$_SESSION['sessMsg'] = " Newsletter Send To Selected Subscriber(s) Successfully. ";
			header("location: addressBookManager.php");
			exit();
		}
		
		if($Submit == 'Active')
		{
			$sqlA = " update tbl_address_book set status = 1 where id in ($strIds) ";
			executeQuery($sqlA);
			$_SESSION['sessMsg'] = " Selected Record(s) Are Now Active. ";
			header("location:addressBookManager.php");
			exit();
		}
		
		if($Submit == 'Inactive')
		{
			$sqlA = " update tbl_address_book set status = 0 where id in ($strIds) ";
			executeQuery($sqlA);
			$_SESSION['sessMsg'] = " Selected Record(s) Are Now Inactive. ";
			header("location: addressBookManager.php");
			exit();
		}
		
		if($Submit == 'Delete')
		{
			$sqlA = " delete from tbl_address_book where id in ($strIds) ";
			executeQuery($sqlA);
			
			$_SESSION['sessMsg'] = " Selected Record(s) Are Deleted Successfully. ";
			header("location: addressBookManager.php");
			exit();
		}
	}
	else
	{
		$_SESSION['sessMsg'] = " Please select at least one checkbox for desire action. ";
		header("location: addressBookManager.php");
		exit;
	}
}

/* $order_by = $_GET['order_by'];
$order_by2 = $_GET['order_by2'];

if($order_by=='') $order_by="sellerId";
if($order_by2=='') $order_by2="desc"; */
$listPaging = $_REQUEST['listPaging'];
if($page == '') { $page = 1; }
if($pagegroup == '') { $pagegroup = 1; }
if(trim($listPaging) == '') { $listPaging = 20; }

$sqlPL = " select * from tbl_address_book where 1 = 1  ";

@extract($_REQUEST);

$sqlPL.= concatSql($isSql=false,$sqlVal='',$tblField='firstName',$requestField='sFirstName',$method='get',$whCondition='and',$whOpreator='like');
$sqlPL.= concatSql($isSql=false,$sqlVal='',$tblField='middleName',$requestField='sMiddleName',$method='get',$whCondition='and',$whOpreator='like');
$sqlPL.= concatSql($isSql=false,$sqlVal='',$tblField='lastName',$requestField='sLastName',$method='get',$whCondition='and',$whOpreator='like');
$sqlPL.= concatSql($isSql=false,$sqlVal='',$tblField='organizationName',$requestField='sOrganizationName',$method='get',$whCondition='and',$whOpreator='like');
$sqlPL.= concatSql($isSql=false,$sqlVal='',$tblField='organizationType',$requestField='sOrganizationType',$method='get',$whCondition='and',$whOpreator='=');

$sqlPL.= concatSql($isSql=false,$sqlVal='',$tblField='email',$requestField='sEmailAddress',$whCondition='get',$andOr='and',$type='like');
$sqlPL.=" order by id desc";

$resPL = executeQuery(dopaging($sqlPL,$listPaging));
?>